package com.ruoyi.business.service.impl;

import java.rmi.server.ServerCloneException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import com.ruoyi.business.domain.vo.BatchGoods;
import com.ruoyi.business.domain.vo.GoodTreeDto;
import com.ruoyi.business.domain.vo.GoodsTree;
import com.ruoyi.common.annotation.CommunityScope;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BizGoodsMapper;
import com.ruoyi.business.domain.BizGoods;
import com.ruoyi.business.service.IBizGoodsService;

/**
 * 商品列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-22
 */
@Service
public class BizGoodsServiceImpl implements IBizGoodsService 
{
    @Autowired
    private BizGoodsMapper bizGoodsMapper;

    /**
     * 查询商品列表
     * 
     * @param goodsId 商品列表主键
     * @return 商品列表
     */
    @Override
    public BizGoods selectBizGoodsByGoodsId(Long goodsId)
    {
        return bizGoodsMapper.selectBizGoodsByGoodsId(goodsId);
    }

    /**
     * 查询商品列表列表
     * 
     * @param bizGoods 商品列表
     * @return 商品列表
     */
    @Override
    @CommunityScope
    public List<BizGoods> selectBizGoodsList(BizGoods bizGoods)
    {
        return bizGoodsMapper.selectBizGoodsList(bizGoods);
    }

    /**
     * 新增商品列表
     * 
     * @param bizGoods 商品列表
     * @return 结果
     */
    @Override
    public int insertBizGoods(BizGoods bizGoods)
    {
        bizGoods.setAudit("0");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(loginUser.getUser().isAdmin()) {//管理员
            bizGoods.setAudit("1");
        }
        else {
            for (SysRole role : loginUser.getUser().getRoles()) {
                String dataScope = role.getCommunityDataScope();
                //全部数据权限
                if ("1".equals(dataScope)) {
                    bizGoods.setAudit("1");
                    break;
                }
            }
        }
        bizGoods.setCreateTime(DateUtils.getNowDate());
        return bizGoodsMapper.insertBizGoods(bizGoods);
    }

    /**
     * 修改商品列表
     * 
     * @param bizGoods 商品列表
     * @return 结果
     */
    @Override
    public int updateBizGoods(BizGoods bizGoods)
    {
        bizGoods.setAudit("0");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(loginUser.getUser().isAdmin()) {//管理员
            bizGoods.setAudit("1");
        }
        else {
            for (SysRole role : loginUser.getUser().getRoles()) {
                String dataScope = role.getCommunityDataScope();
                //全部数据权限
                if ("1".equals(dataScope)) {
                    bizGoods.setAudit("1");
                    break;
                }
            }
        }
        bizGoods.setUpdateTime(DateUtils.getNowDate());
        return bizGoodsMapper.updateBizGoods(bizGoods);
    }

    /**
     * 批量删除商品列表
     * 
     * @param goodsIds 需要删除的商品列表主键
     * @return 结果
     */
    @Override
    public int deleteBizGoodsByGoodsIds(Long[] goodsIds)
    {
        return bizGoodsMapper.deleteBizGoodsByGoodsIds(goodsIds);
    }

    /**
     * 删除商品列表信息
     * 
     * @param goodsId 商品列表主键
     * @return 结果
     */
    @Override
    public int deleteBizGoodsByGoodsId(Long goodsId)
    {
        return bizGoodsMapper.deleteBizGoodsByGoodsId(goodsId);
    }

    /**
     * 按社区显示的商品
     *
     * @return
     */
    @Override
    @CommunityScope(alias = "a")
    public List<GoodsTree> goodTree(GoodTreeDto goodTreeDto) {
        BizGoods bizGoods = new BizGoods();
        if (!goodTreeDto.getParams().isEmpty()) {
            bizGoods.setParams(goodTreeDto.getParams());
        }
        List<GoodsTree> trees = new ArrayList<>();
        List<BizGoods> lists = bizGoodsMapper.selectBizGoodsList(bizGoods);
        long var = 10_000L;
        for (BizGoods list : lists) {
            GoodsTree exist = trees.stream()
                    .filter(c -> c.getId().equals(list.getCommunityId()*var))
                    .findFirst()
                    .orElse(null);
            if (exist==null) {
                GoodsTree tree = new GoodsTree();
                tree.setId(list.getCommunityId()*var);
                tree.setLabel(list.getCommunityName());
                trees.add(tree);
            }
        }
        for (GoodsTree tree : trees) {
            List<GoodsTree.InnerGoods> innerGoods = new ArrayList<>();
            for (BizGoods list : lists) {
                if (tree.getId().equals(list.getCommunityId()*var)) {
                    GoodsTree.InnerGoods tmp = new GoodsTree.InnerGoods();
                    tmp.setId(list.getGoodsId());
                    tmp.setLabel(list.getTitle());
                    innerGoods.add(tmp);
                }
            }
            tree.setChildren(innerGoods);
        }
        return trees;
    }

    /**
     * p批量修改商品
     *
     * @param batchGoods
     * @return
     */
    @Override
    public int batchGoods(BatchGoods batchGoods) {
        long var = 10_000L;
        if (Objects.isNull(batchGoods.getGoodsId()) || batchGoods.getGoodsId().size()==0) {
            throw new ServiceException("批量修改的商品不能空");
        }
        List<Long> ids = new ArrayList<>();
        for (Long l : batchGoods.getGoodsId()) {
            if (l>var) continue;
            ids.add(l);
        }
        batchGoods.setGoodsId(ids);
        batchGoods.setUpdateTime(DateUtils.getNowDate());
        return this.bizGoodsMapper.batchGoods(batchGoods);
    }

    /**
     * 批量审核商品
     *
     * @param username
     * @return
     */
    @Override
    public int batchAudit(String username) {
        boolean auditRole = false;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(loginUser.getUser().isAdmin()) {//管理员
            auditRole = true;
        }
        else {
            for (SysRole role : loginUser.getUser().getRoles()) {
                String dataScope = role.getCommunityDataScope();
                //全部数据权限
                if ("1".equals(dataScope)) {
                    auditRole = true;
                    break;
                }
            }
        }
        if (!auditRole) {
            throw new ServiceException("您当前没有权限批量审核");
        }
        BizGoods batchGoods = new BizGoods();
        batchGoods.setAudit("0");
        List<BizGoods> bizGoods = this.bizGoodsMapper.selectBizGoodsList(batchGoods);
        if (Objects.isNull(bizGoods) || bizGoods.size()==0 ) {
            throw new ServiceException("没有需要审核的商品");
        }
        for (BizGoods bizGood : bizGoods) {
            bizGood.setAudit("1");
            bizGood.setUpdateBy(username);
            bizGood.setUpdateTime(DateUtils.getNowDate());
            this.bizGoodsMapper.updateBizGoods(bizGood);
        }
        return 1;
    }
}
